package bussinessService.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import module.Employees;
import module.Shop;
import utils.DateUtil;
import bussinessService.employeesBussinessService;
import domain.penum.RoleEnum;
import domain.vo.EmployeesVO;
import domain.vo.queryObject;
import exception.EmployeeNoExistException;
/**
 * 
* @ClassName: employeesBussinessService
* @Description: 员工业务层实现
* @author chenchao
* @date 2013-10-31 上午06:12:48
*
 */
public class EmployeesBussinessServiceImpl implements employeesBussinessService {

	/* (non-Javadoc)
	 * @see bussinessService.basicbussinessService#deleteT(int)
	 */

	public void deleteT(int id) throws SQLException {
		// TODO Auto-generated method stub
		
	}


	/* (non-Javadoc)
	 * @see bussinessService.basicbussinessService#getAll()
	 */
	public List<EmployeesVO> getAll() throws SQLException {
		List<EmployeesVO> vos = new ArrayList<EmployeesVO>();
		List<Employees> employeesList = new ArrayList<Employees>();
		employeesList =  employeesService.getAll();
		EmployeesVO tvo = new EmployeesVO();
		tvo.setE_name("经纪人不限");
		tvo.setE_sn("");
		tvo.setSelected(true);
		vos.add(tvo);
		for (Employees e : employeesList) {
			if (!"sysadmin".equals(e.getRole().toLowerCase())) {
				EmployeesVO vo = new EmployeesVO();
				vo.setE_name(e.getE_name());
				vo.setE_sn(e.getE_sn());
				vo.setE_id(Integer.toString(e.getE_id()));
				vos.add(vo);
			}
		}
		return vos;
	}

	/* (non-Javadoc)
	 * @see bussinessService.basicbussinessService#getById(java.lang.String)
	 */
	public Employees getById(String id) throws SQLException {
		// TODO Auto-generated method stub
		return employeesService.getById(id);
	}

	/* (non-Javadoc)
	 * @see bussinessService.basicbussinessService#merageObject(java.lang.Object)
	 */
	public void merageObject(Employees employees) throws SQLException {
		Shop shop = shopService.getById(Integer.toString(employees.getShopId()));
		employees.setShopName(shop.getShopName());
		if(employees.getE_id() == 0 ) {
			employeesService.insertObject(employees);
		} else {
			employeesService.updateObject(employees);
		}
		
	}

	public int countByQueryObject(queryObject queryObject) throws SQLException{
		// TODO Auto-generated method stub
		return employeesService.countByQueryObject(queryObject);
	}

	public List<EmployeesVO> getObjectByQueryObject(queryObject queryObject) throws SQLException {
		// TODO Auto-generated method stub
		List<Employees> emps =  employeesService.getObjectByQueryObject(queryObject);
		List<Employees> results = new LinkedList<Employees>();
		for(Employees emp : emps) {
			if ( -1 != emp.getG_id()){
				results.add(emp);
			}
		}
		
		List<EmployeesVO> vos = new ArrayList<EmployeesVO>();
		for (Employees e : results) {
			EmployeesVO vo = new EmployeesVO();
			vo.setE_name(e.getE_name());
			vo.setE_sn(e.getE_sn());
			vo.setE_status((e.getE_status().equals(Employees.ACTIVITY)? "激活":"冻结"));
			vo.setE_phone(e.getE_phone());
			vo.setShopName(e.getShopName());
			vo.setRole(RoleEnum.getName(Integer.valueOf(e.getRole())));
			vo.setPosition(e.getPosition());
			vo.setVolcation(e.getVolcation());
			vo.setQq(e.getQq());
			vo.setWork(e.getWork());
			vo.setGender(e.getGender());
			vo.setIdcard(e.getIdcard());
			vo.setStartDay(e.getStartDay());
			vo.setEmployeeMark(e.getEmployeeMark());
			vo.setCreateTime(DateUtil.date2String(e.getCreateTime(),
					DateUtil.YYYY_MM_DD));
			vo.setE_id(Integer.toString(e.getE_id()));
			vo.setRoleId(e.getRole());
			vo.setE_photo(e.getE_photo());
			vo.setShopId(e.getShopId());
			vos.add(vo);

		}
		return vos;
	}

	public boolean validateUserExist(queryObject queryObject) throws SQLException {
		if(countByQueryObject(queryObject) != 0) {
			return true;
		} 
		return false;
	}
	

	/* (non-Javadoc)
	 * @see bussinessService.employeesBussinessService#getEmployees(domain.vo.queryObject)
	 */
	public Employees getEmployeeByQueryObject(queryObject queryObject) throws SQLException,
			EmployeeNoExistException {
		if(validateUserExist(queryObject)) {
			EmployeesVO vo = getObjectByQueryObject(queryObject).get(0);
			Employees emp = new Employees();
			emp.setE_sn(vo.getE_sn());
			emp.setShopId(vo.getShopId());
			emp.setE_id(Integer.parseInt(vo.getE_id()));
			return  emp;
		} else  {
			throw new EmployeeNoExistException();
		}
	}
}
